package com.liugh.service.zhongche.Imp;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.liugh.entity.User;
import com.liugh.entity.standard.StandardItem;
import com.liugh.entity.standard.StandardPage;
import com.liugh.mapper.zhongche.StandardCategoryMapper;
import com.liugh.mapper.zhongche.StandardItemMapper;
import com.liugh.mapper.zhongche.StandardPageMapper;
import com.liugh.service.IUserService;
import com.liugh.service.zhongche.IStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StandardItemServiceImp extends ServiceImpl<StandardItemMapper,StandardItem> implements IStandardService {

    @Autowired
    private StandardItemMapper itemMapper;


    @Autowired
    private IUserService userService;

    @Autowired
    private StandardPageMapper standardPageMapper;


    @Autowired
    private StandardCategoryMapper standardCategoryMapper;

    @Override
    public StandardItem update(StandardItem item,User user) {
        item.setUpdateTime(new Date());
        item.setAuthor(user.getUserNo());
        if (!updateById(item)) {
            item = null;
        }
        return item;
    }

    @Override
    public StandardItem add(StandardItem item, User user) {
        Date date = new Date();
        item.setUpdateTime(date);
        item.setCreateTime(date);
        item.setAuthor(user.getUserNo());
        item.setApproveStatus(-1);
        item.setCategoryName(standardCategoryMapper.selectById(item.getCategory()).getCategoryName());
        if (!insert(item)) {
            item = null;
        }
        return item;
    }

    /**
     * 通过lib_type选择条目
     * @param itemPage
     * @param type
     * @return
     */
    @Override
    public Page<StandardItem> selectByLibType(Page<StandardItem> itemPage,int type) {
        //默认选择现行库和参选库
        if (type == 0) {
            itemPage = selectPage(itemPage,new EntityWrapper<StandardItem>().where("lib_type != 2 "));
        }
            itemPage = selectPage(itemPage,new EntityWrapper<StandardItem>().where("lib_type = {0}",type));
        return itemPage;
    }

    /**
     * 选择所有条目
     * @param itemPage
     * @return
     */
    @Override
    public Page<StandardItem> getAllItemPage(Page<StandardItem> itemPage) {
        List<StandardItem> standardItemList = itemMapper.selectPage(itemPage, new EntityWrapper<StandardItem>().orderBy("crate_time",false));
        for (StandardItem standardItem:standardItemList) {
            standardItem.setUserName(userService.getUserByUserNo(standardItem.getAuthor()).getUsername());
            standardItem.setCategoryName(standardCategoryMapper.selectById(standardItem.getCategory()).getCategoryName());
        }
        return itemPage.setRecords(standardItemList);
    }

    /**
     * 获取我上传的item
     * @param
     * @return
     */
    @Override
    public Page<StandardItem> getMyUploadItem(Page<StandardItem> page,String userNo) {
        List<StandardItem> standardItemList = itemMapper.selectPage(page, new EntityWrapper<StandardItem>().where("author = {0}", userNo).orderBy("create_time", false));
        for (StandardItem standardItem:standardItemList) {
            standardItem.setUserName(userService.getUserByUserNo(standardItem.getAuthor()).getUsername());
            standardItem.setCategoryName(standardCategoryMapper.selectById(standardItem.getCategory()).getCategoryName());
        }
        page.setRecords(standardItemList);
        return page;
    }


    /**
     * 返回包含十二条的图片 .
     * @param id .
     * @return .
     */
    public StandardItem getItemById(int id) {
        List<StandardPage> standardPageList = standardPageMapper.selectList(new EntityWrapper<StandardPage>().eq("standard_item_id", id).last("limit 12"));
        StandardItem standardItem = itemMapper.selectById(id);
        standardItem.setStandardPages(standardPageList);
        if (standardItem.getAuthor() != null) {
            standardItem.setUserName(userService.getUserByUserNo(standardItem.getAuthor()).getUsername());
        }
        standardItem.setCategoryName(standardCategoryMapper.selectById(standardItem.getCategory()).getCategoryName());
        return standardItem;

    }


}
